엑셀시트 복사 두번째 :: JSP 일반[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

JSP 일반
[1]
등록일:2010-08-10 23:51:06 (0%)
작성자:
제목:엑셀시트 복사 두번째
  1. import java.util.Collection;   
  2. import java.util.HashMap;   
  3. import java.util.Map;   
  4. import java.util.Set;   
  5. import java.util.TreeSet;   
  6. import org.apache.poi.hssf.usermodel.HSSFCell;   
  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  8. import org.apache.poi.hssf.usermodel.HSSFRow;   
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  10. import org.apache.poi.hssf.util.Region;   
  11.   
  12. /**  
  13.  *  
  14.  * @author jk  
  15.  * getted from http://jxls.cvs.sourceforge.net/jxls/jxls/src/java/org/jxls/util/Util.java?revision=1.8&view=markup  
  16.  * by Leonid Vysochyn   
  17.  * and modified (adding styles copying)  
  18.  */  
  19. public class Util {   
  20.     public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet){   
  21.         copySheets(newSheet, sheet, true);   
  22.     }   
  23.     public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, boolean copyStyle){   
  24.         int maxColumnNum = 0;   
  25.         Map<Integer, HSSFCellStyle> styleMap = (copyStyle)   
  26.                 ? new HashMap<Integer, HSSFCellStyle>() : null;   
  27.   
  28.         for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {   
  29.             HSSFRow srcRow = sheet.getRow(i);   
  30.             HSSFRow destRow = newSheet.createRow(i);   
  31.             if (srcRow != null) {   
  32.                 Util.copyRow(sheet, newSheet, srcRow, destRow, styleMap);   
  33.                 if (srcRow.getLastCellNum() > maxColumnNum) {   
  34.                     maxColumnNum = srcRow.getLastCellNum();   
  35.                 }   
  36.             }   
  37.         }   
  38.         for (int i = 0; i <= maxColumnNum; i++) {   
  39.             newSheet.setColumnWidth(i, sheet.getColumnWidth(i));   
  40.         }   
  41.     }   
  42.   
  43.     public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, Map<Integer, HSSFCellStyle> styleMap) {   
  44.         Set mergedRegions = new TreeSet();   
  45.         destRow.setHeight(srcRow.getHeight());   
  46.         for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {   
  47.             HSSFCell oldCell = srcRow.getCell(j);   
  48.             HSSFCell newCell = destRow.getCell(j);   
  49.             if (oldCell != null) {   
  50.                 if (newCell == null) {   
  51.                     newCell = destRow.createCell(j);   
  52.                 }   
  53.                 copyCell(oldCell, newCell, styleMap);   
  54.                 Region mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(), oldCell.getCellNum());   
  55.                 if (mergedRegion != null) {   
  56. //                    Region newMergedRegion = new Region( destRow.getRowNum(), mergedRegion.getColumnFrom(),   
  57. //                            destRow.getRowNum() + mergedRegion.getRowTo() - mergedRegion.getRowFrom(), mergedRegion.getColumnTo() );   
  58.                     Region newMergedRegion = new Region(mergedRegion.getRowFrom(), mergedRegion.getColumnFrom(),   
  59.                             mergedRegion.getRowTo(), mergedRegion.getColumnTo());   
  60.                     if (isNewMergedRegion(newMergedRegion, mergedRegions)) {   
  61.                         mergedRegions.add(newMergedRegion);   
  62.                         destSheet.addMergedRegion(newMergedRegion);   
  63.                     }   
  64.                 }   
  65.             }   
  66.         }   
  67.            
  68.     }   
  69.     public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {   
  70.         if(styleMap != null) {   
  71.             if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){   
  72.                 newCell.setCellStyle(oldCell.getCellStyle());   
  73.             } else{   
  74.                 int stHashCode = oldCell.getCellStyle().hashCode();   
  75.                 HSSFCellStyle newCellStyle = styleMap.get(stHashCode);   
  76.                 if(newCellStyle == null){   
  77.                     newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();   
  78.                     newCellStyle.cloneStyleFrom(oldCell.getCellStyle());   
  79.                     styleMap.put(stHashCode, newCellStyle);   
  80.                 }   
  81.                 newCell.setCellStyle(newCellStyle);   
  82.             }   
  83.         }   
  84.         switch(oldCell.getCellType()) {   
  85.             case HSSFCell.CELL_TYPE_STRING:   
  86.                 newCell.setCellValue(oldCell.getStringCellValue());   
  87.                 break;   
  88.             case HSSFCell.CELL_TYPE_NUMERIC:   
  89.                 newCell.setCellValue(oldCell.getNumericCellValue());   
  90.                 break;   
  91.             case HSSFCell.CELL_TYPE_BLANK:   
  92.                 newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);   
  93.                 break;   
  94.             case HSSFCell.CELL_TYPE_BOOLEAN:   
  95.                 newCell.setCellValue(oldCell.getBooleanCellValue());   
  96.                 break;   
  97.             case HSSFCell.CELL_TYPE_ERROR:   
  98.                 newCell.setCellErrorValue(oldCell.getErrorCellValue());   
  99.                 break;   
  100.             case HSSFCell.CELL_TYPE_FORMULA:   
  101.                 newCell.setCellFormula(oldCell.getCellFormula());   
  102.                 break;   
  103.             default:   
  104.                 break;   
  105.         }   
  106.            
  107.     }   
  108.     public static Region getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {   
  109.         for (int i = 0; i < sheet.getNumMergedRegions(); i++) {   
  110.             Region merged = sheet.getMergedRegionAt(i);   
  111.             if (merged.contains(rowNum, cellNum)) {   
  112.                 return merged;   
  113.             }   
  114.         }   
  115.         return null;   
  116.     }   
  117.   
  118.     private static boolean isNewMergedRegion(Region region, Collection mergedRegions) {   
  119.         return !mergedRegions.contains(region);   
  120.     }   
  121. }  
[본문링크] 엑셀시트 복사 두번째
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=31586
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.